package b.f;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

/* compiled from: ArraySet */
public final class c<E> implements Collection<E>, Set<E> {
    public static final int[] e = new int[0];
    public static final Object[] f = new Object[0];
    public static Object[] g;
    public static int h;
    public static Object[] i;
    public static int j;
    public int[] a = e;
    public Object[] b = f;
    public int c = 0;
    public g<E, E> d;

    public static void c(int[] iArr, Object[] objArr, int i) {
        if (iArr.length == 8) {
            synchronized (c.class) {
                if (j < 10) {
                    objArr[0] = i;
                    objArr[1] = iArr;
                    for (i--; i >= 2; i--) {
                        objArr[i] = null;
                    }
                    i = objArr;
                    j++;
                }
            }
        } else if (iArr.length == 4) {
            synchronized (c.class) {
                if (h < 10) {
                    objArr[0] = g;
                    objArr[1] = iArr;
                    for (i--; i >= 2; i--) {
                        objArr[i] = null;
                    }
                    g = objArr;
                    h++;
                }
            }
        }
    }

    public boolean add(E e) {
        int e2;
        int i;
        if (e == null) {
            e2 = e();
            i = 0;
        } else {
            e2 = e.hashCode();
            i = e2;
            e2 = d(e, e2);
        }
        if (e2 >= 0) {
            return false;
        }
        e2 = ~e2;
        int i2 = this.c;
        int[] iArr = this.a;
        if (i2 >= iArr.length) {
            int i3 = 4;
            if (i2 >= 8) {
                i3 = (i2 >> 1) + i2;
            } else if (i2 >= 4) {
                i3 = 8;
            }
            Object[] objArr = this.b;
            b(i3);
            int[] iArr2 = this.a;
            if (iArr2.length > 0) {
                System.arraycopy(iArr, 0, iArr2, 0, iArr.length);
                System.arraycopy(objArr, 0, this.b, 0, objArr.length);
            }
            c(iArr, objArr, this.c);
        }
        int i4 = this.c;
        if (e2 < i4) {
            int[] iArr3 = this.a;
            int i5 = e2 + 1;
            System.arraycopy(iArr3, e2, iArr3, i5, i4 - e2);
            Object[] objArr2 = this.b;
            System.arraycopy(objArr2, e2, objArr2, i5, this.c - e2);
        }
        this.a[e2] = i;
        this.b[e2] = e;
        this.c++;
        return true;
    }

    public boolean addAll(Collection<? extends E> collection) {
        int size = collection.size() + this.c;
        int[] iArr = this.a;
        boolean z = false;
        if (iArr.length < size) {
            Object[] objArr = this.b;
            b(size);
            size = this.c;
            if (size > 0) {
                System.arraycopy(iArr, 0, this.a, 0, size);
                System.arraycopy(objArr, 0, this.b, 0, this.c);
            }
            c(iArr, objArr, this.c);
        }
        for (Object add : collection) {
            z |= add(add);
        }
        return z;
    }

    public final void b(int i) {
        Object[] objArr;
        if (i == 8) {
            synchronized (c.class) {
                objArr = i;
                if (objArr != null) {
                    this.b = objArr;
                    i = (Object[]) objArr[0];
                    this.a = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    j--;
                    return;
                }
            }
        } else if (i == 4) {
            synchronized (c.class) {
                objArr = g;
                if (objArr != null) {
                    this.b = objArr;
                    g = (Object[]) objArr[0];
                    this.a = (int[]) objArr[1];
                    objArr[1] = null;
                    objArr[0] = null;
                    h--;
                    return;
                }
            }
        }
        this.a = new int[i];
        this.b = new Object[i];
    }

    public void clear() {
        int i = this.c;
        if (i != 0) {
            c(this.a, this.b, i);
            this.a = e;
            this.b = f;
            this.c = 0;
        }
    }

    public boolean contains(Object obj) {
        return indexOf(obj) >= 0;
    }

    public boolean containsAll(Collection<?> collection) {
        for (Object contains : collection) {
            if (!contains(contains)) {
                return false;
            }
        }
        return true;
    }

    public final int d(Object obj, int i) {
        int i2 = this.c;
        if (i2 == 0) {
            return -1;
        }
        int a = d.a(this.a, i2, i);
        if (a < 0 || obj.equals(this.b[a])) {
            return a;
        }
        int i3 = a + 1;
        while (i3 < i2 && this.a[i3] == i) {
            if (obj.equals(this.b[i3])) {
                return i3;
            }
            i3++;
        }
        a--;
        while (a >= 0 && this.a[a] == i) {
            if (obj.equals(this.b[a])) {
                return a;
            }
            a--;
        }
        return ~i3;
    }

    public final int e() {
        int i = this.c;
        if (i == 0) {
            return -1;
        }
        int a = d.a(this.a, i, 0);
        if (a < 0 || this.b[a] == null) {
            return a;
        }
        int i2 = a + 1;
        while (i2 < i && this.a[i2] == 0) {
            if (this.b[i2] == null) {
                return i2;
            }
            i2++;
        }
        a--;
        while (a >= 0 && this.a[a] == 0) {
            if (this.b[a] == null) {
                return a;
            }
            a--;
        }
        return ~i2;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Set) {
            Set set = (Set) obj;
            if (this.c != set.size()) {
                return false;
            }
            int i = 0;
            while (i < this.c) {
                try {
                    if (!set.contains(this.b[i])) {
                        return false;
                    }
                    i++;
                } catch (ClassCastException | NullPointerException unused) {
                }
            }
            return true;
        }
        return false;
    }

    public E f(int i) {
        Object[] objArr = this.b;
        E e = objArr[i];
        int i2 = this.c;
        if (i2 <= 1) {
            c(this.a, objArr, i2);
            this.a = e;
            this.b = f;
            this.c = 0;
        } else {
            int[] iArr = this.a;
            int i3 = 8;
            if (iArr.length <= 8 || i2 >= iArr.length / 3) {
                i2--;
                this.c = i2;
                if (i < i2) {
                    int i4 = i + 1;
                    System.arraycopy(iArr, i4, iArr, i, i2 - i);
                    Object[] objArr2 = this.b;
                    System.arraycopy(objArr2, i4, objArr2, i, this.c - i);
                }
                this.b[this.c] = null;
            } else {
                if (i2 > 8) {
                    i3 = i2 + (i2 >> 1);
                }
                b(i3);
                this.c--;
                if (i > 0) {
                    System.arraycopy(iArr, 0, this.a, 0, i);
                    System.arraycopy(objArr, 0, this.b, 0, i);
                }
                i2 = this.c;
                if (i < i2) {
                    int i5 = i + 1;
                    System.arraycopy(iArr, i5, this.a, i, i2 - i);
                    System.arraycopy(objArr, i5, this.b, i, this.c - i);
                }
            }
        }
        return e;
    }

    public int hashCode() {
        int[] iArr = this.a;
        int i = 0;
        for (int i2 = 0; i2 < this.c; i2++) {
            i += iArr[i2];
        }
        return i;
    }

    public int indexOf(Object obj) {
        return obj == null ? e() : d(obj, obj.hashCode());
    }

    public boolean isEmpty() {
        return this.c <= 0;
    }

    public Iterator<E> iterator() {
        if (this.d == null) {
            this.d = new b(this);
        }
        g gVar = this.d;
        if (gVar.b == null) {
            gVar.b = new b.f.g.c();
        }
        return gVar.b.iterator();
    }

    public boolean remove(Object obj) {
        int indexOf = indexOf(obj);
        if (indexOf < 0) {
            return false;
        }
        f(indexOf);
        return true;
    }

    public boolean removeAll(Collection<?> collection) {
        boolean z = false;
        for (Object remove : collection) {
            z |= remove(remove);
        }
        return z;
    }

    public boolean retainAll(Collection<?> collection) {
        boolean z = false;
        for (int i = this.c - 1; i >= 0; i--) {
            if (!collection.contains(this.b[i])) {
                f(i);
                z = true;
            }
        }
        return z;
    }

    public int size() {
        return this.c;
    }

    public Object[] toArray() {
        int i = this.c;
        Object[] objArr = new Object[i];
        System.arraycopy(this.b, 0, objArr, 0, i);
        return objArr;
    }

    public String toString() {
        if (isEmpty()) {
            return "{}";
        }
        StringBuilder stringBuilder = new StringBuilder(this.c * 14);
        stringBuilder.append('{');
        for (int i = 0; i < this.c; i++) {
            if (i > 0) {
                stringBuilder.append(", ");
            }
            c cVar = this.b[i];
            if (cVar != this) {
                stringBuilder.append(cVar);
            } else {
                stringBuilder.append("(this Set)");
            }
        }
        stringBuilder.append('}');
        return stringBuilder.toString();
    }

    public <T> T[] toArray(T[] tArr) {
        Object tArr2;
        if (tArr2.length < this.c) {
            tArr2 = (Object[]) Array.newInstance(tArr2.getClass().getComponentType(), this.c);
        }
        System.arraycopy(this.b, 0, tArr2, 0, this.c);
        int length = tArr2.length;
        int i = this.c;
        if (length > i) {
            tArr2[i] = null;
        }
        return tArr2;
    }
}
